﻿using AntdUI;
using CKS.MES.Server.Demo;
using Microsoft.Extensions.Configuration;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace CKS.MES.Server
{
    public partial class PlcTable : UserControl
    {
        public PlcTable()
        {
            InitializeComponent();
            this.Load += PlcPage_Load;
            InitTable();
        }

        private void PlcPage_Load(object sender, EventArgs e)
        {
            if (DesignMode)
                return;

        }

        List<PlcInfoUIItem> DataSource = new List<PlcInfoUIItem>();

        void InitTable()
        {
            table1.EditMode = TEditMode.None;
            table1.Columns = new ColumnCollection();
            table1.Columns.Add(new Column("Code", "工位") { Width = "80" }  );
            table1.Columns.Add(new Column("Name", "工位名称"));
            table1.Columns.Add(new Column("State", "状态") { Width = "80" });
            table1.Columns.Add(new Column("OkCount", "OK数量"));
            table1.Columns.Add(new Column("NgCount", "NG数量"));

            DataSource.Add(new PlcInfoUIItem("OP10")
            {
                Id =1,
                ConnectionState = new CellTag[] {
                new CellTag("在线") {  Type = TTypeMini.Primary}  }
                ,HeartState = new CellBadge(TState.Processing) { Fill = Color.Green , DotRatio = 0.6f  }
            });
            DataSource.Add(new PlcInfoUIItem("OP20")
            {
                Id = 2,
                ConnectionState = new CellTag[] {
                new CellTag("在线") {  Type = TTypeMini.Primary}  }
                ,
                HeartState = new CellBadge(TState.Processing) { Fill = Color.Green }
            });
            DataSource.Add(new PlcInfoUIItem("OP30")
            {
                Id = 3,
                ConnectionState = new CellTag[] {
                new CellTag("在线") {  Type = TTypeMini.Primary}  },
                HeartState = new CellBadge(TState.Processing) { Fill = Color.Green }
            });
            DataSource.Add(new PlcInfoUIItem("OP40")
            {
                Id=4,
                ConnectionState = new CellTag[] {
                new CellTag("离线") {  Type = TTypeMini.Error}  }
                ,
                HeartState = new CellBadge(TState.Default) { Fill = Color.Gray }
            });


            for (int i = 5; i < 12; i++)
            {
                DataSource.Add(new PlcInfoUIItem($"OP{i}0")
                {
                    Id = i,
                    ConnectionState = new CellTag[] {
                    new CellTag("在线") {  Type = TTypeMini.Primary}  },
                    HeartState = new CellBadge(TState.Processing) { Fill = Color.Green }
                });
            }
            

            table1.DataSource = DataSource;

        }

        internal void UpdateState(List<int> onlineIds)
        {


            foreach (var item in DataSource) {
                if(onlineIds.FindIndex(a => a == item.Id) <0)
                {
                    item.IsOnline = true;
                  
                }
                else
                {
                    item.IsOnline = true;
                    
                }
                
            
            }

            UpdateContentLanguage();
        }



        IConfigurationRoot configLanguage = null;

        internal void UpdateLanguage(IConfigurationRoot configBuilder)
        {
            configLanguage = configBuilder;
            foreach (var item in table1.Columns)
            {
                switch (item.Key)
                {
                    case "Code":
                        item.Title = configBuilder["plcTable:workstationCodeHeader"];
                        break;
                    case "Name":
                        item.Title = configBuilder["plcTable:workstationNameHeader"];
                        break;
                    case "State":
                        item.Title = configBuilder["plcTable:statusHeader"];
                        break;
                    case "OkCount":
                        item.Title = configBuilder["plcTable:OkCountHeader"];
                        break;
                    case "NgCount":
                        item.Title = configBuilder["plcTable:NgCountHeader"];
                        break;
                    default:
                        break;
                }
            }

            UpdateContentLanguage();


        }

        /// <summary>
        /// 更新内容语言
        /// </summary>
        public void UpdateContentLanguage()
        {
            if (configLanguage == null)
                return;
            foreach (var row in DataSource)
            {
                string status = "";
                TTypeMini TType = TTypeMini.Primary;
                if (row.IsOnline)
                {
                    status = configLanguage["plcTable:onlineStatus"];
                }
                else
                {
                    TType = TTypeMini.Error;
                    status = configLanguage["plcTable:offlineStatus"];
                }
                row.ConnectionState = new CellTag[] {
                    new CellTag(status) {  Type = TType }  };
            }
        }
    }


   
}
